# API文档地址： https://github.com/argosopentech/argos-translate
# 模型下载地址：https://www.argosopentech.com/argospm/index/
from email.policy import default
from argostranslate import package, translate

# 显示已有模型
# print(package.get_available_packages())
# 载入模型文件
# package.install_from_path(r'D:\pybox\QtTest\Way2\model\translate-ja_en-1_1.argosmodel')
# # 查询语种
# installed_languages = translate.get_installed_languages()
# print( [str(lang) for lang in installed_languages] )
# # 获取相应语种的翻译器 from - to
# translation_en_es = installed_languages[16].get_translation(installed_languages[0])
# # 翻译
# print(translation_en_es.translate("あなたが好き"))
# 中-3 英-0 日-16 韩-17
LANS = [3, 0, 16, 17]
MIDDLEWARE = 0  # 中间转换语言
default_options = {
    'from': LANS[2],
    'to': LANS[0]
}
class ArgoTran:
    options = default_options
    client = None
    translation = [None, None]
    IS_ONCE = False

    def __init__(self, config):
        self.client = translate.get_installed_languages()
        self.initTran(config)
        print(package.get_available_packages())

    def initTran(self, config):
        t = config['SOURCE_LANGUAGE']
        if t == 0:
            pass
        else:
            self.options['from'] =  LANS[t-1]
        self.options['to'] =  LANS[config['TARGET_LANGUAGE']]
        self.IS_ONCE = self.options['from'] == MIDDLEWARE | self.options['to'] == MIDDLEWARE
        if self.IS_ONCE:
            # 包含中间语言只需要一个翻译器
            self.translation[0] = self.client[self.options['from']].get_translation(self.client[self.options['to']])
        else:
            self.translation[0] = self.client[self.options['from']].get_translation(self.client[MIDDLEWARE])
            self.translation[1] = self.client[MIDDLEWARE].get_translation(self.client[self.options['to']])

    def initOptions(self, config):
        self.initTran(config)

    def trans(self, content):
        if self.IS_ONCE:
            return self.translation[0].translate(content)
        else:
            mid = self.translation[0].translate(content)
            return self.translation[1].translate(mid)


